package chinatelecom.email_contact;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import net.dongliu.requests.RequestBuilder;
import net.dongliu.requests.Requests;
import net.dongliu.requests.json.TypeInfer;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Spider {
    private static String url = "https://mail.chinatelecom.cn/webmail/getContact.do?oper=getNode&group=group";
    private static String emailListUrl = "https://mail.chinatelecom.cn/webmail/entaddrlist.do?act=doDepUserList&groupId=%s&page=%s";

    private static Map<String, String> headers = new HashMap<>();
    private static List<ContactBean> results = new ArrayList<>();

    static {
        headers.put("Cookie", "DGAKSUVBXTMNEKLSJF=95d17d0fa1ffadfe1735eda1236ccebda6c3600e6bb3f3d38a031add321cf771da9dd7e4429780945bcb71eb3460a691; cLoginNameEntLogin=; remUserCheckEntLogin=false; remSslCheckEntLogin=true; taoxiaoqing.jschinatelecom.cnlabelDisplay=1; taoxiaoqing.jschinatelecom.cnMailFrmRowSameLable=*,16; taoxiaoqing.jschinatelecom.cnMailHeadDisplay=; taoxiaoqing.jschinatelecom.cnpopDisplay=1; taoxiaoqing.jschinatelecom.cnMailFrmRow=*,16; taoxiaoqing.jschinatelecom.cnfldDisplay=1; JSESSIONID=aaaKYScwze6867eEr4Esx; loginStartTime=Sat Sep 26 2020 09:55:09 GMT+0800 (中国标准时间); WEBADMIN_EMAIL_ACCOUNT_2019=vT3aBrrQKzhF9UP21yWZDGaLsgJp6ocHGT1V5ekKgDU=; SESSION_ID=e1bccc9e30775f327c1adf6a95fbbe76; hermes_logout_url=https://mail.chinatelecom.cn/webmail/dxmail/login.html; WEBMAIL_ACCOUNT_2019=HwUF9y4l+OCRDtDnPJV64hR4tj5PlCz1fuwSuQez9+w=; WEBMAIL_HOST=https://mail.chinatelecom.cn/webmail/createNewMail.do; ENTSERVERLEVEL=3; HMM_CSRF_TOKEN=eLmiiz299zaDZEM3; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=zh_CN");
        headers.put("Host", "mail.chinatelecom.cn");
    }

    public static void main(String[] args) {
//        String departure = "";

        spider(url);
        FileUtil.writeLines(results, "d:\\tmp\\contacts.csv", Charset.forName("gbk"));
//        getEmailList();
    }

    private static void spider(String url) {
//首层
        System.out.println("爬虫现在开始");
        RequestBuilder req = Requests.get(url);
        System.out.println("请求url：" + req.toString());
        req.headers(headers);
        List<ContactBean> result = req.send().readToJson(new TypeInfer<List<ContactBean>>() {
        });
        System.out.println("获取部门信息：" + result.toString());
        for (ContactBean contactBean : result) {
            getDepartureList(contactBean.getId(), contactBean.getName(), String.valueOf(0));
        }
    }


    private static void getDepartureList(String id, String title, String level) {
//        if (!title.contains("集团总部")) {
//            return;
//        }
        System.out.println("获取部门信息：" + title.toString());
        if (!title.contains("专业公司及运营单位")) {
            return;
        }

        Map<String, String> postParams = new HashMap<>();
        postParams.put("id", id);
        postParams.put("n", title);
        postParams.put("lv", level);

        RequestBuilder req = Requests.post(url);
        req.headers(headers);
        req.params(postParams);
        List<ContactBean> departureList = req.send().readToJson(new TypeInfer<List<ContactBean>>() {
        });
//获取当前机构下的email地址列表
        getEmailList(id, title);
        for (ContactBean contactBean : departureList) {
            if (contactBean.getName().contains("天翼智慧家庭科技有限公司")||title.contains("天翼智慧家庭科技有限公司")) {
                getDepartureList(contactBean.getId(), title + "-" + contactBean.getName(), String.valueOf(Integer.parseInt(level) + 1));
            }
        }
    }


    private static void getEmailList(String groupId, String title) {
        RequestBuilder req = Requests.get(String.format(emailListUrl, groupId, 1));
        req.headers(headers);
        //获取总页数
        String body = req.send().readToText();
        int pageCount = Integer.parseInt(getPageCount(body));
        for (int i = 1; i <= pageCount; i++) {
            RequestBuilder emailListReq = Requests.get(String.format(emailListUrl, groupId, i));
            emailListReq.headers(headers);
            String emailListBody = emailListReq.send().readToText();
            parseEmailList(emailListBody, title);
        }
    }

    private static String getPageCount(String body) {
        try {
            Document doc = Jsoup.parse(body);
            Elements elements = doc.getElementsByAttributeValue("name", "turnPage");
            String pageCount = elements.get(0).childNode(1).childNode(0).toString();
            if (pageCount.contains("/")) {
                String[] pages = pageCount.split("/");
                return pages[1];
            }
            return pageCount;
        } catch (Exception e) {
            return "1";
        }
    }

    private static void parseEmailList(String body, String title) {
        Document doc = Jsoup.parse(body);
        Elements elements = doc.getElementsByClass("TRxg");
        for (Element element : elements) {
            Elements couple = element.select(".ListSpace");
            String name = StringUtils.trim(couple.get(0).childNode(0).childNode(0).toString());
            String email = StringUtils.trim(couple.get(1).childNode(0).childNode(0).toString());
            results.add(new ContactBean(name, title, email));
            System.out.println(title + "-" + name + ":" + email);
        }
    }
}
